package com.ponhu.ea2.stockcenter.controller;

import com.ponhu.ea2.stockcenter.service.bms.StockCommonService;
import com.ponhu.ea2.stockcenter.vo.ReturnResponseBean;
import com.ponhu.ea2.stockcenter.vo.StockMqVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/common")
@Api(tags = "公共接口", value = "CommonController")
public class CommonController {

    @DubboReference(version = "1.0.0", check = false)
    private StockCommonService stockCommonService;

    /**
     * 查询可卖库存信息：可售库存+虚拟可售库存-销售占用库存
     *
     * @param inventoryCodes
     * @return
     */
    @ApiOperation(value = "查询可卖库存信息：可售库存+虚拟可售库存-销售占用库存")
    @GetMapping("/getSaleStockMqVoList")
    public ReturnResponseBean<List<StockMqVO>> getSaleStockMqVoList(@RequestParam Collection<String> inventoryCodes) {
        if (CollectionUtils.isEmpty(inventoryCodes))
            return ReturnResponseBean.error("货品编码集合不可为空");

        return ReturnResponseBean.ok(stockCommonService.getSaleStockMqVoList(inventoryCodes));
    }

    /**
     * 根据指定RedisKey获取缓存信息
     */
    @ApiOperation(value = "根据指定RedisKey获取缓存信息")
    @GetMapping("/getValueFromRedisBucket")
    public ReturnResponseBean<String> getValueFromRedisBucket(@RequestParam String redisKey,
                                                              @RequestParam(required = false, defaultValue = "false") Boolean getAndDel) {

        return ReturnResponseBean.ok(stockCommonService.getValueFromRedisBucket(redisKey, getAndDel));
    }

    /**
     * 测试Redis事件监听
     */
    @ApiOperation(value = "测试Redis事件监听")
    @GetMapping("/testRedisKeyEvent")
    public ReturnResponseBean testRedisKeyEvent(@RequestParam String redisKey,
                                                @RequestParam String redisValue, @RequestParam Long timeToLive) {

        return stockCommonService.testRedisKeyEvent(redisKey, redisValue, timeToLive) ? ReturnResponseBean.ok() : ReturnResponseBean.error();
    }

}
